package test.workload;

import test.envset.GlobalState;
import test.utility.Randomly;
import test.utility.SqlCollections;

/*
 *@ 2023-06-26
 * 运行SQL的工作线程
 */
public class RunWorker extends Worker {
    private static SqlCollections sqls = null;
    private boolean execSucc = false;

    public static void setSqlCollections(SqlCollections sqls) {
        RunWorker.sqls = sqls;
    }

    public RunWorker(int id, GlobalState globalState) {
        this.id = id;
        this.globalState = globalState;
    }


    @Override
    public void work() {
        if (sqls == null) {
            System.out.println("[Error:]:RunWorker. sqls not set");
            return;
        }
        while (!GlobalState.getTerminateFlag()) {
            counter[0]++;
            String sql = Randomly.fromList(sqls.getSqls());
//GlobalState. output(sql);
            execSucc = globalState.executeSelect(sql);
            if (execSucc) {
                counter[1]++;
            } else {
                counter[2]++;
            }
        }
    }
}
